package conMgr.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import conMgr.model.Conference;
import conMgr.model.User;
import conMgr.model.UserType;
import conMgr.ui.util.SelectUserView;
import conMgr.ui.util.Ui;

/**
 * Represent specific conference information 
 * @author Soon
 *
 */
@SuppressWarnings("serial")
public class NewConferenceInfoView extends JPanel
{

	
	/**
	 * date pattern.
	 */
	private static final String datePattern = "MM-dd-yyyy";
	
	/**
	 * The program chair
	 */
	private User programChair;
	
	/**
	 * The date formatter.
	 */
	private final SimpleDateFormat formatter;
	
	/**
	 * The new conference
	 */
	private Conference newConf;
	
	/**
	 * The name of the conference text box
	 */
	private JTextComponent nameTextbox;
	
	/**
	 * The submission deadline text box.
	 */
	private JTextComponent submissionDeadlineTextbox;
	
	/**
	 * The review deadline text box
	 */
	private JTextComponent reviewDeadlineTextbox;
	
	/**
	 * The revision deadline text box.
	 */
	private JTextComponent revisionDeadlineTextbox;
	
	/**
	 * The recommendation deadline text box.
	 */
	private JTextComponent recommendationDeadlineTextbox;
	
	/**
	 * The program chair's name text box.
	 */
	private JTextComponent programChairTextbox;

	/**
	 * Initializes a new conference info view.
	 * @param conference
	 * @throws ParseException
	 */
	public NewConferenceInfoView(final Conference conference) throws ParseException
	{
		final GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
	
		newConf = conference;
		formatter = new SimpleDateFormat(datePattern);
		addGridBagFieldLabel("New Conference's Title:", 0);
		addGridBagFieldLabel("Submission Deadline (mm-dd-yyyy):", 1);
		addGridBagFieldLabel("Review Deadline (mm-dd-yyyy):", 2);
		addGridBagFieldLabel("Recommendation Deadine (mm-dd-yyyy):", 3);
		addGridBagFieldLabel("Revision Deadine (mm-dd-yyyy):", 4);
		addGridBagFieldLabel("Program Chair's Name:", 5);
		
		nameTextbox = addGridBagValueTextBox(0, ((newConf != null ? newConf.getName() : "")));
		submissionDeadlineTextbox = addGridBagValueTextBox(1, (newConf != null ? formatter.format(newConf.getSubmissionDL()) : ""));
		reviewDeadlineTextbox = addGridBagValueTextBox(2, (newConf != null ? formatter.format(newConf.getReviewDL()) : ""));
		revisionDeadlineTextbox = addGridBagValueTextBox(3, (newConf != null ? formatter.format(newConf.getRevisionDL()) : ""));
		recommendationDeadlineTextbox = addGridBagValueTextBox(4, (newConf != null ? formatter.format(newConf.getRecommendationDL()) : ""));
		programChairTextbox = addGridBagValueTextBox(5, ((newConf != null ? newConf.getProgramChair().getName() : "")));
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = Ui.DEFAULT_INSETS;
		constraints.gridx = 3;
		constraints.gridy = 5;
		constraints.anchor = GridBagConstraints.WEST;
		add(Ui.createButton("...", new PickUserAction()), constraints);
		
	}
	/**
	 * Provides an action for the "..." button. 
	 */
	class PickUserAction extends AbstractAction
	{
		/**
		 * The serialization ID of this class.
		 */
		private static final long serialVersionUID = 7536822585168351327L;

		/**
		 * Handles the action. Shows the SelectUserView.
		 */
		@Override
		public void actionPerformed(final ActionEvent arg0)
		{
			final User user = SelectUserView.getUser(UserType.PROGRAM_CHAIR);
			
			if (user == null)
				return;
			
			NewConferenceInfoView.this.programChairTextbox.setText(user.getName());
			NewConferenceInfoView.this.programChair = user;
		}
	}
	
	/**
	 * Adds a new empty text box to a grid bag.
	 * @param row The row of the grid that the text box belongs in.
	 * @param text The text to put in the text box.
	 * @return The newly-created text box.
	 */
	private JTextComponent addGridBagValueTextBox
	(
		final int row,
		final String text
	)
	{
		JComponent addControl;
		JTextComponent field;
		
		addControl = field = new JTextField(20);
		
		if (newConf != null)
		{
			field.setText(text);
			field.setBackground(Color.WHITE);
		}
		final GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 2;
		constraints.gridy = row;
		constraints.anchor = GridBagConstraints.WEST;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets = Ui.DEFAULT_INSETS;
		add(addControl, constraints);
		
		return field;
	}

	/**
	 * Add the field of conference information.
	 * @param the_text The text of label.
	 * @param row The row of GridBagConstraints.
	 */
	private void addGridBagFieldLabel(final String the_text, final int row)
	{
		final JLabel label = new JLabel(the_text);
		final GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = row;
		constraints.anchor = GridBagConstraints.EAST;
		constraints.insets = Ui.DEFAULT_INSETS;
		add(label, constraints);
		
	}
	
	/**
	 * Creates a new conference with the information entered in the text boxes.
	 * @return The newly created paper or null if errors were encountered.
	 * @throws ParseException 
	 */
	public Conference createConference() throws ParseException
	{
		final String title = nameTextbox.getText();
		final Date submissionDeadline = formatter.parse(submissionDeadlineTextbox.getText());
		final Date reviewDeadline = formatter.parse(reviewDeadlineTextbox.getText());
		final Date revisionDeadline = formatter.parse(revisionDeadlineTextbox.getText());
		final Date recommendationDeadline = formatter.parse(recommendationDeadlineTextbox.getText());
		for (User user : User.getAllUsers())
			if (user.getType().equals(UserType.PROGRAM_CHAIR) &&
					user.getName().equals(programChairTextbox.getText()))
				programChair = user;
				
		if
		(
			isBlank(title) ||
			isBlank(submissionDeadline) ||
			isBlank(reviewDeadline) ||
			isBlank(revisionDeadline) ||
			isBlank(recommendationDeadline)||
			isBlank(programChair)
		)
		{
			JOptionPane.showMessageDialog
			(
				null,
				"Please fill out all of the fields. A paper cannot be created with missing information.",
				"Cannot Submit Paper",
				JOptionPane.ERROR_MESSAGE
			);
			return null;
		}
		
		return new Conference(title, programChair, submissionDeadline, reviewDeadline, recommendationDeadline, revisionDeadline);
	}
	
	/**
	 * Modifies new conference view.
	 * @throws ParseException
	 */
	public void modifyConference() throws ParseException
	{
		final String title = nameTextbox.getText();
		final Date submissionDeadline = formatter.parse(submissionDeadlineTextbox.getText());
		final Date reviewDeadline = formatter.parse(reviewDeadlineTextbox.getText());
		final Date revisionDeadline = formatter.parse(revisionDeadlineTextbox.getText());
		final Date recommendationDeadline = formatter.parse(recommendationDeadlineTextbox.getText());
		for (User user : User.getAllUsers())
			if (user.getType().equals(UserType.PROGRAM_CHAIR) &&
					user.getName().equals(programChairTextbox.getText()))
				programChair = user;
		if
		(
			isBlank(title) ||
			isBlank(submissionDeadline) ||
			isBlank(reviewDeadline) ||
			isBlank(revisionDeadline) ||
			isBlank(recommendationDeadline)||
			isBlank(programChair)
		)
		{
			JOptionPane.showMessageDialog
			(
				null,
				"Please fill out all of the fields. A paper cannot be created with missing information.",
				"Cannot Submit Paper",
				JOptionPane.ERROR_MESSAGE
			);
		}
		newConf.setName(nameTextbox.getText());
		newConf.setSubmissionDL(submissionDeadline);
		newConf.setReviewDL(reviewDeadline);
		newConf.setRevisionDL(revisionDeadline);
		newConf.setSubmissionDL(submissionDeadline);
		newConf.setProgramChair(programChair);
	}
	/**
	 * Gets a value indicating if the given string is blank.
	 * @param text The string to test.
	 * True if the string is null or empty; otherwise, false. 
	 */
	private boolean isBlank(final Object text)
	{
		return text == null || text.equals("");
	}
	
	
}

